package com.quickdev.intelligenthome.config
{
	import flash.events.Event;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;

	public class Config
	{
		public static var serviceUrl :String;
		public static var port :uint = 0;
		public static var userNo :String;
		public static var delay :uint = 5000;
		public static var repeatCount :uint = 3;
		
		public function Config()
		{
		}
		
		public static function loadConfig( configFileUrl :String ) :void {
			var urlReq :URLRequest = new URLRequest( configFileUrl );
			urlReq.contentType = URLLoaderDataFormat.TEXT;
			var loader :URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onLoadConfig);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadError);
			loader.load( urlReq );
		}
		
		private static function onLoadConfig( event :Event ) :void {
			var loader :URLLoader = event.target as URLLoader;
			loader.removeEventListener(Event.COMPLETE, onLoadConfig);
			loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadError);
			
			var xml :XML = new XML( loader.data );
			serviceUrl = xml.Service[0].@url;
			port = parseInt(xml.Service[0].@port);
			userNo = xml.UserNo[0].children()[0];
		}
		
		private static function onLoadError( event :SecurityErrorEvent ) :void {
			var loader :URLLoader = event.target as URLLoader;
			loader.removeEventListener(Event.COMPLETE, onLoadConfig);
			loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoadError);
			trace( event.toString() );
		}
	}
}